package com.xc.hotelmgr.order.modules.mt.service;

import com.alibaba.fastjson.JSONObject;
import com.xc.hotelmgr.order.common.utils.Constant;
import com.xc.hotelmgr.order.common.utils.MtR;
import com.xc.hotelmgr.order.modules.ali.service.DeliGoodsService;
import com.xc.hotelmgr.order.modules.mt.to.notice.Notice;
import com.xc.hotelmgr.order.modules.order.service.OrderService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Service("mtStatusChangeCallbackService")
public class MtStatusChangeCallbackService {
    @Autowired
    private DeliGoodsService deliGoodsService;

    @Autowired
    private OrderService orderService;
    /**
     * 发货
     * @param notice
     * @return
     */
    public MtR doChange(Notice notice) {
        //21 预订成功 book_suc
        //22 预订失败 book_fail
        //31 取消成功 cancel_suc
        //40 美团客服介入退款 abort
        //50 已入住 book_checkin
        if(notice.getOrderStatus() == null){
            return MtR.ok();
        }
        Constant.MtOrderStatus mtOrderStatus = Constant.MtOrderStatus.getStatusByValue(notice.getOrderStatus());
        Map<String,Object> params = null;
        switch (mtOrderStatus){
            case  book_suc :
                deliGoodsService.deliGood(notice.getDistributorOrderId(), Constant.OptType.Confirm,notice.getMtOrderId().toString());
                break;
            case book_fail://更新订单采购状态为采购失败
                params = new HashMap<>();
                params.put("orderCode",notice.getDistributorOrderId());
                params.put("newBuyOrderId",notice.getMtOrderId().toString());
                params.put("orderStatus",Constant.LocalOrdBuyStatus.Purchased);
                params.put("buyStatus",Constant.LocalOrdBuyStatus.Purchfailure.getValue());
                params.put("resultDesc", "美团自动采购失败");
                deliGoodsService.updateOrderStatus(params);
                break;
            case cancel_suc:
                params = new HashMap<>();
                params.put("orderCode",notice.getDistributorOrderId());
                params.put("orderStatus",Constant.LocalOrderStatus.Refund);
                deliGoodsService.updateOrderStatus(params);
                break;
            case book_checkin:
                params = new HashMap<>();
                params.put("orderCode",notice.getDistributorOrderId());
                params.put("orderStatus",Constant.LocalOrderStatus.CheckIn);
                deliGoodsService.updateOrderStatus(params);
                break;
        }
        return MtR.ok();
    }


    public MtR doChangeJson(JSONObject jsonObject) {
        //21 预订成功 book_suc
        //22 预订失败 book_fail
        //31 取消成功 cancel_suc
        //40 美团客服介入退款 abort
        //50 已入住 book_checkin
        if(jsonObject == null || StringUtils.isBlank(jsonObject.getString("distributorOrderId"))){
            return MtR.ok();
        }
        Constant.MtOrderStatus mtOrderStatus = Constant.MtOrderStatus.getStatusByValue(jsonObject.getInteger("orderStatus"));
        Map<String,Object> params = null;
        switch (mtOrderStatus){
            case  book_suc :
                deliGoodsService.deliGood(jsonObject.getLong("distributorOrderId"), Constant.OptType.Confirm,jsonObject.getString("mtOrderId"));
                break;
            case book_fail://更新订单采购状态为采购失败
                params = new HashMap<>();
                params.put("orderCode",jsonObject.getString("distributorOrderId"));
                params.put("newBuyOrderId",jsonObject.getString("mtOrderId"));
                params.put("orderStatus",Constant.LocalOrdBuyStatus.Purchased);
                params.put("buyStatus",Constant.LocalOrdBuyStatus.Purchfailure.getValue());
                params.put("resultDesc", "美团自动采购失败");
                deliGoodsService.updateOrderStatus(params);
                break;
            case cancel_suc:
                params = new HashMap<>();
                params.put("orderCode",jsonObject.getString("distributorOrderId"));
                params.put("orderStatus",Constant.LocalOrderStatus.Refund.getValue());
                orderService.updateOlnyOrderStatus(params);
                break;
            case book_checkin:
                params = new HashMap<>();
                params.put("orderCode",jsonObject.getString("distributorOrderId"));
                params.put("orderStatus",Constant.LocalOrderStatus.CheckIn.getValue());
                orderService.updateOlnyOrderStatus(params);
                break;
        }
        return MtR.ok();
    }
}
